package com.example.bdmanagerapplication

import android.Manifest
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.FragmentActivity
import com.baidu.location.BDLocation
import com.baidu.location.BDLocationListener
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.CoordType
import com.baidu.mapapi.SDKInitializer
import com.baidu.mapapi.map.MapStatus
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.example.bdmanagerapplication.databinding.ActivityMainBinding


import com.tbruyelle.rxpermissions3.RxPermissions
import org.jetbrains.anko.startActivity

class MainActivity : AppCompatActivity() {
    private val mLocationClient by lazy { LocationClient(this) }
    private var isFirstLoc = true
    private lateinit var mBinding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        mBinding=ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        LocationClient.setAgreePrivacy(true)
        mBinding.bmapView.map.isMyLocationEnabled = true
        checkVersion()
        mBinding.mAddBtn.setOnClickListener {
            startActivity<SecondActivity>()
        }
        mBinding.mRecordsBtn.setOnClickListener {
            startActivity<RecordsActivity>()
        }
    }
    @SuppressLint("CheckResult")
    private fun checkVersion() {

        val rxPermissions = RxPermissions(this)

        rxPermissions.request(
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
        ).subscribe { granted: Boolean ->
                if (granted) { //申请成功
                    //发起连续定位请求
                    getLocation() // 定位初始化
                } else { //申请失败
                    //showToast("权限未开启")
                }
            }
    }
    fun getLocation() {
        //定位初始化
        //定位初始化


//通过LocationClientOption设置LocationClient相关参数

//通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)

//设置locationClientOption

//设置locationClientOption
        mLocationClient.locOption = option

//注册LocationListener监听器

//注册LocationListener监听器
        mLocationClient.registerLocationListener(object : BDLocationListener {
            override fun onReceiveLocation(location: BDLocation?) {
                //mapView 销毁后不在处理新接收的位置
                if (location == null || mBinding.bmapView == null) {
                    return
                }
                val locData = MyLocationData.Builder().accuracy(location.radius)
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.direction).latitude(location.latitude)
                    .longitude(location.longitude).build()

                if (isFirstLoc) {
                    isFirstLoc = false
                    Log.d("测试数据",location.toString())
                    mBinding.bmapView.map.setMyLocationData(locData)
                    //val latLng = LatLng(location.latitude, location.longitude)
                    //MyLog.d(getGson().toJson(location))

                    //getMarkList(location.latitude, location.longitude)
                    //latitude = location.latitude
                    //longitude = location.longitude

                    //reverseGeoCode(latLng)

                    val latLng = LatLng(location.latitude, location.longitude)
//                    pushLatitude = location.latitude
//                    pushLongitude = location.longitude
                    //changeMarkPosition(location.latitude, location.longitude, addressName)
                    val builder = MapStatus.Builder()
                    builder.target(latLng).zoom(20.0f)
                    mBinding.bmapView.map.animateMapStatus(
                        MapStatusUpdateFactory.newMapStatus(
                            builder.build()
                        )
                    )


                }

            }

        })
        //开启地图定位图层
        mLocationClient.start()
    }
}